home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / etc / resolvconf / update.d / bind next >
Encoding:
Text File  |  2006-08-09  |  3.5 KB  |  103 lines

  1. #!/bin/bash
  2. # Need bash because we use ${foo//bar/baz}
  3. #
  4. # Script to update the named options file
  5. #
  6. # Resolvconf may run us even if named is not running.
  7. # If a bind package is installed then we go ahead and update
  8. # the named configuration in case named is started later.
  9. #
  10. # Assumption: On entry, PWD contains the resolv.conf-type files
  11. #
  12. # Licensed under the GNU GPL.  See /usr/share/doc/resolvconf/copyright.
  13. #
  14. # Written by Thomas Hood <jdthood@yahoo.co.uk>
  15.  
  16. set -e
  17. PATH=/sbin:/bin
  18.  
  19. [ -x /usr/sbin/named ] || exit 0
  20. [ -x /lib/resolvconf/list-records ] || exit 1
  21. [ -f /etc/bind/named.conf.options ] || exit 0
  22.  
  23. OPTS_FILE=named.options
  24. RUN_DIR=/var/run/bind
  25.  
  26. [ -d "$RUN_DIR" ] || mkdir --parents --mode=0755 "$RUN_DIR"
  27.  
  28. # Stores arguments (minus duplicates) in RSLT, separated by spaces
  29. # Doesn't work properly if an argument itself contain whitespace
  30. uniquify()
  31. {
  32.     RSLT=""
  33.     while [ "$1" ] ; do
  34.         for E in $RSLT ; do
  35.             [ "$1" = "$E" ] && { shift ; continue 2 ; }
  36.         done
  37.         RSLT="${RSLT:+$RSLT }$1"
  38.         shift
  39.     done
  40. }
  41.  
  42. # Get list of records, excluding all those for the loopback interface
  43. RSLVCNFFILES="$(/lib/resolvconf/list-records | sed -e '/^lo$/d' -e '/^lo[.]/d')"
  44.  
  45. ### Compile semicolon-separated list nameservers ###
  46. NMSRVRS=""
  47. if [ "$RSLVCNFFILES" ] ; then
  48.     uniquify $(sed -n -e 's/^[[:space:]]*nameserver[[:space:]]\+//p' $RSLVCNFFILES)
  49.     [ "$RSLT" ] && NMSRVRS="${RSLT// /; }; "
  50. fi
  51.  
  52. # N.B.: After changing directory we no longer have access to the resolv.conf-type files
  53. cd "$RUN_DIR"
  54.  
  55. TMP_FILE="${OPTS_FILE}_new.$$"
  56. clean_up() { rm -f "${RUN_DIR}/$TMP_FILE" ; }
  57. trap clean_up EXIT
  58. rm -f "$TMP_FILE"
  59.  
  60. # We want to process named.conf.options such that the new forwarders 
  61. # statement gets inserted but nothing else is corrupted in the process.
  62. # We want to do this using only commands available in /bin and /sbin, i.e.,
  63. # with sh, sed and/or grep.   Sed can be made to work -- with difficulty.
  64. # Even so, the following script does not work properly if comment
  65. # delimiters of one style of commenting appear inside another kind of
  66. # comment.  (Named supports C, C++ and sh comment styles.)
  67. #
  68. # First, we do our best to delete all and only comments.
  69. # Then we delete any existing forwarders statement, taking into account
  70. # the fact that these can span several lines.  Then we add a new
  71. # forwarders statement at the beginning of the options statement.
  72. #
  73. echo "// named.conf fragment automatically generated by $0" > "$TMP_FILE"
  74. echo "// DO NOT EDIT THIS FILE.  Instead edit /etc/bind/named.conf.options ." >> "$TMP_FILE"
  75. cat /etc/bind/named.conf.options \
  76.     | sed -e 's%\*/%\*/\
  77. %g' \
  78.     | sed -e '\%/\*%{ :x ; s%\*/%\*/% ; t y ; N ; b x ; :y ; s%/\*.*\*/%% ; }' \
  79.     | sed -e 's%//.*%%' -e 's%#.*%%' \
  80.     | sed -e '/forwarders/{ :x ; s/}/}/ ; t y ; N ; b x ; :y ; s/}[[:space:]]*;/};/ ; t z ; N ; b y ; :z s/forwarders[[:space:]]*{[^}]*};// ; }' \
  81.     | sed -e 's/options[[:space:]]*{/options {\
  82.     forwarders { '"${NMSRVRS}"'};/' | sed -e '/^[[:space:]]*$/{ d ; }' \
  83.     >> "$TMP_FILE"
  84.  
  85. # bind version 8 does not create a "bind" group
  86. chown root:bind "$TMP_FILE" > /dev/null 2>&1 || :
  87.  
  88. if [ "$1" = "-i" ] ; then
  89.     mv -f "$TMP_FILE" "$OPTS_FILE"
  90.     exit 0
  91. fi
  92.  
  93. # Reload named unless we know its options haven't changed
  94. if [ -x /usr/bin/diff ] && [ -f "$OPTS_FILE" ] && /usr/bin/diff -q "$OPTS_FILE" "$TMP_FILE" > /dev/null ; then
  95.     # No change
  96.     rm -f "$TMP_FILE"
  97. else
  98.     mv -f "$TMP_FILE" "$OPTS_FILE"
  99.     [ -x /etc/init.d/bind9 ] && /etc/init.d/bind9 reload > /dev/null 2>&1 || :
  100.     [ -x /etc/init.d/bind  ] && /etc/init.d/bind reload > /dev/null 2>&1 || :
  101. fi
  102.  
  103.